package jiejie.jianzhioffer;

public class offer18 {
    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        node.next=node1;
        node1.next=node2;
        node2.next=null;
        ListNode res=deleteNode(node,2);
        while(res!=null){
            System.out.println(res.val);
            res=res.next;
        }
    }

    /**
     * 删除链表指定的节点
     * @param head
     * @param val
     * @return
     */
    public static ListNode deleteNode(ListNode head, int val) {
        if(head==null || head.next==null)
            return head;
        if(head.val==val)
            return head.next;
        ListNode node=head;
        while(node.next.val!=val)
            node=node.next;
        node.next=node.next.next;
        return head;
    }
}